(function () {
    let watermarkRule = null
    function chooseRule(waterMarkInfo, rules) {
        if (rules === null) {
            return null
        }
        var url = waterMarkInfo.url
        var port = url.port
        if (!port) {
            if (url.protocol === 'https:') {
                port = '443'
            } else {
                port = '80'
            }
        }
        var host = url.hostname + ':' + port
        for (let i = 0; i < rules.length; i++) {
            var rulehost = rules[i].host

            if (rulehost.indexOf(':') < 0) {
                rulehost = rulehost + ':' + port
            }
            if (host === rulehost) {
                return rules[i]
            }
        }
        return null
    }

    function contentReplace(waterMarkInfo,rule) {
        var v = {
            user: waterMarkInfo.masterAccount,
            username: waterMarkInfo.userName,
            clientip: rule.clientIp,
            time: rule.time
        }
        var c = rule.content
        for (var key in v) {
            c = c.replace("{" + key + "}", v[key])
        }
        return c
    }

    function createElement(ssoInfo, rule) {
        var id = 'watermark';
        if (document.getElementById(id) !== null) {
            document.body.removeChild(document.getElementById(id))
        }
        var str = contentReplace(ssoInfo, rule);
        var canvasEle = document.createElement('canvas');
        canvasEle.width = rule.width;
        canvasEle.height = rule.height;
        var ctx = canvasEle.getContext('2d');
        ctx.translate(canvasEle.width / 2, canvasEle.height / 2);
        ctx.rotate(-Number(rule.rotation) * Math.PI / 180);
        ctx.translate(canvasEle.width / -2, canvasEle.height / -2);
        ctx.font = rule.size + 'px ' + rule.font;
        ctx.globalAlpha = rule.transparency;
        ctx.fillStyle = rule.color;
        ctx.textAlign = 'left';
        ctx.fillText(str, canvasEle.width / 16, canvasEle.height / 2);
        var divEle = document.createElement('div');
        divEle.id = id;
        divEle.style.width = document.documentElement.scrollWidth - 10 + 'px';
        divEle.style.height = document.documentElement.scrollHeight - 20 + 'px';
        divEle.style.pointerEvents = 'none';
        divEle.style.position = 'absolute';
        divEle.style.top = '3px';
        divEle.style.left = '5px';
        divEle.style.background = 'url(' + canvasEle.toDataURL() + ') left top repeat';
        divEle.style.zIndex = 9999999;
        document.body.appendChild(divEle);

    }

    async function watermarkFun() {
        if (window.parent != window) {
            return;
        }
        var waterMarkInfo = await ipc.invoke('getWaterMarkInfo')
        if (watermarkRule === null) {
            var res = await fetch('/apvgw/api/watermark')
            if (res.status !== 200) {
                document.removeChild(document)
                return
            }
            var data = await res.json()
            if (data.code != 0) {
                document.removeChild(document)
                return
            }
            watermarkRule = chooseRule(waterMarkInfo, data.rules)
            if (watermarkRule === null) {
                return;
            }
            watermarkRule['time'] = data.time
            watermarkRule['clientIp'] = data.clientIp
            setTimeout(function(){
                watermarkRule = null
                watermarkFun()
            }, watermarkRule.refresh * 60 * 1000)
        }

        if (watermarkRule === null) {
            return;
        }
        createElement(waterMarkInfo, watermarkRule)
    };

    window.addEventListener('resize', function(){
        watermarkFun();
    })
    window.addEventListener('scroll', function(){
        watermarkFun();
    })
    window.addEventListener('load', function(){
        watermarkFun();
    })
    console.log('test111')
})()